﻿using NPOI.DDF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MDT.LifeSewagePlatform.Conmmon
{
    public static class  IntExtension
    {
        private const int HourTransformMinute = 60;
        private const int DayTransformHour = 24;
        public static int GetQuarter(this int month)
        {
            int divid = month / 3;
            int remainder = month % 3;
            if (remainder > 0)
            {
                divid++;
            }
            return divid;
        }
        /// <summary>
        /// a相对于b增长百分比
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static double GetPercent(this double a,double b,int length=2)
        { double res=0;
            if (b!=0 && a!=0)
            {
                res=(a - b)/ b*100;
            }
            return Math.Round(res,length);
        }
        public static string GetPercentStr(this double a, double b, int length = 2)
        {
            double res = 0;
            if (b != 0 && a != 0)
            {
                res = (a - b) / b * 100;
            }
            return Math.Abs(res).SaveNumberPoint(length);
        }

        public static double GetPercentBySun(this double a, double Sum, int length = 2)
        {
            double res = 0;
            if (Sum != 0 && a!=0)
            {
                res = a/ Sum * 100;
            }
            return Math.Round(res, length);
        }

        public static string GetPercentBySunStr(this double a, double Sum, int length = 2)
        {
            double res = 0;
            if (Sum != 0 && a != 0)
            {
                res = a / Sum * 100;
            }
            return res.SaveNumberPoint(length);
        }

        public static string MappingZero(this double number,int length=2)
        {
            string ND = "ND";
            if (number>0)
            {
                ND = number.SaveNumberPoint(length).ToString();
            }
            return ND;
        }
        public static string MapMaxNumber(this int number)
        {
            string str = string.Empty;
            switch (number)
            {
                case 1:
                    str = "一";
                    break;
                case 2:
                    str = "二";
                    break;
                case 3:
                    str = "三";
                    break;
                case 4:
                    str = "四";
                    break;
                case 5:
                    str = "五";
                    break;
                case 6:
                    str = "六";
                    break;
                case 7:
                    str = "七";
                    break;
                case 8:
                    str = "八";
                    break;
                case 9:
                    str = "九";
                    break;
                case 10:
                    str = "十";
                    break;
                case 11:
                    str = "十一";
                    break;
                case 12:
                    str = "十二";
                    break;
                case 13:
                    str = "十三";
                    break;
                case 14:
                    str = "十四";
                    break;
                case 15:
                    str = "十五";
                    break;
                default:
                    break;
            }
            return str;
        }

        public static string GetRandom(this int length) 
        {
            Random random = new Random();
            string str = string.Empty;
            for (int i = 0; i <length; i++)
            {
                str += random.Next(0, 10);
            }
            return str;
        }
        //
        public static double GetDivision(this double avg,double a,int length=2)
        {
            double res = 0;
            if (avg!=0 && a!=0)
            {
                res=a / avg;
            }
            return Math.Round(res,length);
        }
        public static string GetDivisionStr(this double avg, double a, int length = 2)
        {
            double res = 0;
            if (avg != 0 && a != 0)
            {
                res = a / avg;
            }
            return res.SaveNumberPoint(length);
        }

        public static int GetPastQuarter(this int quarter)
        {
            if (quarter==1)
            {
                return 4;
            }
            return quarter-1;
        }

        public static int GetByPastQuarterYear(this int quarter,int year)
        {
            if (quarter == 1)
            {
                return year - 1;
            }
            return year;
        }

        public static string GetPercentDesc(this double percent)
        {
            string desc = string.Empty;
            if (percent>0)
            {
                desc = $"上涨{percent}%";
            }
            if (percent==0)
            {
                desc = "未变动";

            }
            if (percent<0)
            {
                desc = $"下跌{percent}%";
            }
            return desc;
        }

        //public static string GetByUpDown(this double a,double b)
        //{
        //    if ()
        //    {

        //    }
        //}
        
        public static int GetChangeTypeID(this double percent)
        {
            if (percent>0)
            {
                return (int)ChangeType.Up;
            }
            else if (percent==0)
            {
                return (int)ChangeType.Same;
            }
            else
            {
                return (int)ChangeType.Down;
            }
        }
        /// <summary>
        /// 精准保留小数
        /// </summary>
        /// <param name="number"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string SaveNumberPoint(this double number,int length=2)
        {
            StringBuilder sb = new StringBuilder("0.");
            if (length>0)
            {
                for (int i = 0; i < length; i++)
                {
                    sb.Append("0");
                }
            }
            return number.ToString($"#{sb}");
        }

        public static int GetPastMonth(this int month)
        {
            if (month==1)
            {
                return 12;
            }
            return month - 1;
        }

        public static int GetPastYearByMonth(this int month,int year)
        {
            if (month == 1)
            {
                return year-1;
            }
            return year;
        }
        /// <summary>
        /// int ip 转换为 字符串
        /// </summary>
        /// <param name="ipInt"></param>
        /// <returns></returns>
        public static string IntToIp(this long ipInt)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append((ipInt >> 24) & 0xFF).Append(".");
            sb.Append((ipInt >> 16) & 0xFF).Append(".");
            sb.Append((ipInt >> 8) & 0xFF).Append(".");
            sb.Append(ipInt & 0xFF);
            return sb.ToString();
        }

        public static float Divide(this float num,int passiveNum,int length=2)
        {
            if (num==0 || passiveNum == 0)
            {
                return 0;
            }
            double res = num / passiveNum;
            return  Convert.ToSingle(Math.Round(res,length));
        }

        public static float Divide(this int num, float passiveNum, int length = 2)
        {
            if (num == 0 || passiveNum == 0)
            {
                return 0;
            }
            double res = num / passiveNum;
            return Convert.ToSingle(Math.Round(res, length));
        }

        public static float GetMinuteDiff(this DateTime startTime, DateTime endTime)
        {
            TimeSpan diff = endTime - startTime;
            float minutes = diff.Minutes;
            if (diff.Days > 0)
                minutes += diff.Days * HourTransformMinute * DayTransformHour;
            if (diff.Hours > 0)
                minutes += diff.Hours * HourTransformMinute;
            if (diff.Seconds > 0)
                minutes += diff.Seconds / (float)HourTransformMinute;
            return minutes;
        }
    }
}
